package dao.user;

import domain.User;
import util.DButils;

import javax.swing.plaf.nimbus.State;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao{
    /**
     * 添加社区信息
     * @param username
     * @param password
     * @return int
     * @author 夏添
     * @date 2020/9/17 15:58
     */
    public User verifyUser(String username, String password) throws Exception{
        User user =new User();
        String sql = "Select * from User where username='"+username+"' and password='"+password+"'";
        Connection conn = DButils.getConn();
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery(sql);
        while (rs.next()){
            user.setUser_id(rs.getInt("user_id"));
            user.setUsername(rs.getString("username"));
            user.setPassword(rs.getString("password"));
            user.setName(rs.getString("name"));
        }
        DButils.close(rs,st,conn);
        return user;
    }

    /**
     * 修改密码
     * @param userId
     * @param newPassword
     * @return int
     * @author 夏添
     * @date 2020/9/17 16:09
     */
    public int updatePassword(int userId, String newPassword) throws Exception{
        String sql="update User set password='"+newPassword+"' where user_id="+userId;
        Connection conn = DButils.getConn();
        Statement st = conn.createStatement();
        int i = st.executeUpdate(sql);
        DButils.close(st,conn);
        return i;
    }

    /**
     * 更新登录时间
     * @param userId
     * @return int
     * @author 夏添
     * @date 2020/9/23 15:39
     */
    public int updateLoginTime(int userId,String logintime) throws Exception{
        String sql = "update User set login_time='"+logintime+"' where user_id="+userId;
        Connection conn = DButils.getConn();
        Statement st = conn.createStatement();
        int i = st.executeUpdate(sql);
        DButils.close(st,conn);
        return i;
    }

    /**
     * 获取用户列表
     * @return List<User>
     * @author 夏添
     * @date 2020/9/23 16:03
     */
    public List<User> getUserList() throws Exception{
        String sql="select * from User";
        List<User> list= new ArrayList<>();
        Connection conn = DButils.getConn();
        Statement st =conn.createStatement();
        ResultSet rs = st.executeQuery(sql);
        while (rs.next()){
            User user = new User();
            user.setUser_id(rs.getInt("user_id"));
            user.setUsername(rs.getString("username"));
            user.setPassword(rs.getString("password"));
            user.setName(rs.getString("name"));
            user.setLogin_time(rs.getString("login_time"));
            list.add(user);
        }
        DButils.close(rs,st,conn);
        return list;
    }

    /**
     * 添加用户
     * @param username
     * @param name
     * @param password
     * @return int
     * @author 夏添
     * @date 2020/9/23 17:26
     */
    public int addUser(String username, String name, String password) throws Exception{
        String sql = "insert User (username,name,password) values('"+username+"','"+name+"','"+password+"')";
        Connection con = DButils.getConn();
        Statement st = con.createStatement();
        int i = st.executeUpdate(sql);
        DButils.close(st,con);
        return i;
    }
}
